/*
 * @(#)CoordSelection.java 1.16 01/12/03 Copyright 2002 Sun Microsystems, Inc.
 * All rights reserved. SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license
 * terms.
 */

/* 
 * ***** BEGIN LICENSE BLOCK ***** Version: NPL 1.1/GPL 2.0/LGPL 2.1 The
 * contents of this file are subject to the Netscape Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/NPL/
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License. The
 * Original Code is lutsko.com code. The Initial Developer of the Original Code
 * is James Lutsko. Portions created by the Initial Developer are Copyright (C)
 * 2003 the Initial Developer. All Rights Reserved. Contributor(s):
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or the
 * GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
 * case the provisions of the GPL or the LGPL are applicable instead of those
 * above. If you wish to allow use of your version of this file only under the
 * terms of either the GPL or the LGPL, and not to allow others to use your
 * version of this file under the terms of the NPL, indicate your decision by
 * deleting the provisions above and replace them with the notice and other
 * provisions required by the GPL or the LGPL. If you do not delete the
 * provisions above, a recipient may use your version of this file under the
 * terms of any one of the NPL, the GPL or the LGPL. ***** END LICENSE BLOCK
 * *****
 */

package autoteam.ui;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

/**
 * A <code>Transferable</code> which implements the capability required to
 * transfer a <code>String</code>. This <code>Transferable</code> properly
 * supports <code>DataFlavor.stringFlavor</code> and all equivalent flavors.
 * Support for <code>DataFlavor.plainTextFlavor</code> and all equivalent
 * flavors is <b>deprecated</b>. No other <code>DataFlavor</code>s are
 * supported.
 * 
 * @see java.awt.datatransfer.DataFlavor.stringFlavor
 * @see java.awt.datatransfer.DataFlavor.plainTextFlavor
 */
public class CoordSelection implements Transferable, ClipboardOwner
{
    
    private Coord data;
    public static DataFlavor coordFlavor = new DataFlavor( Coord.class, "Coord" );
    
    private static final DataFlavor[] flavors = { coordFlavor };
    
    /**
     * Creates a <code>Transferable</code> capable of transferring the specified
     * <code>String</code>.
     */
    public CoordSelection( Object data )
    {
	this.data = (Coord) data;
    }
    
    /**
     * Returns an array of flavors in which this <code>Transferable</code> can
     * provide the data. <code>DataFlavor.stringFlavor</code> is properly
     * supported. Support for <code>DataFlavor.plainTextFlavor</code> is
     * <b>deprecated</b>.
     * 
     * @return an array of length two, whose elements are <code>DataFlavor.
     *         stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
     */
    public DataFlavor[] getTransferDataFlavors( )
    {
	// returning flavors itself would allow client code to modify
	// our internal behavior
	return (DataFlavor[]) flavors.clone( );
    }
    
    /**
     * Returns whether the requested flavor is supported by this
     * <code>Transferable</code>.
     * 
     * @param flavor
     *            the requested flavor for the data
     * @return true if <code>flavor</code> is equal to
     *         <code>DataFlavor.stringFlavor</code> or
     *         <code>DataFlavor.plainTextFlavor</code>; false if
     *         <code>flavor</code> is not one of the above flavors
     * @throws NullPointerException
     *             if flavor is <code>null</code>
     */
    public boolean isDataFlavorSupported( DataFlavor flavor )
    {
	// JCK Test CoordSelection0003: if 'flavor' is null, throw NPE
	for ( int i = 0; i < flavors.length; i++ )
	{
	    if ( flavor.equals( flavors[i] ) )
	    {
		return true;
	    }
	}
	return false;
    }
    
    /**
     * Returns the <code>Transferable</code>'s data in the requested
     * <code>DataFlavor</code> if possible. If the desired flavor is
     * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor, the
     * <code>String</code> representing the selection is returned. If the
     * desired flavor is </code>DataFlavor.plainTextFlavor </code>, or an
     * equivalent flavor, a <code>Reader</code> is returned. <b>Note:<b> The
     * behavior of this method for </code>DataFlavor.plainTextFlavor</code> and
     * equivalent <code>DataFlavor</code>s is inconsistent with the definition
     * of <code>DataFlavor.plainTextFlavor</code>.
     * 
     * @param flavor
     *            the requested flavor for the data
     * @return the data in the requested flavor, as outlined above
     * @throws UnsupportedFlavorException
     *             if the requested data flavor is not equivalent to either
     *             <code>DataFlavor.stringFlavor</code> or
     *             <code>DataFlavor.plainTextFlavor</code>
     * @throws IOException
     *             if an IOException occurs while retrieving the data. By
     *             default, CoordSelection never throws this exception, but a
     *             subclass may.
     * @throws NullPointerException
     *             if flavor is <code>null</code>
     * @see java.io.Reader
     */
    public Object getTransferData( DataFlavor flavor ) throws UnsupportedFlavorException, IOException
    {
	// JCK Test CoordSelection0007: if 'flavor' is null, throw NPE
	// if (flavor.equals(flavors[0])) {
	if ( flavor.equals( coordFlavor ) )
	{
	    return (Object) data;
	}
	else
	{
	    throw new UnsupportedFlavorException( flavor );
	}
    }
    
    public void lostOwnership( Clipboard clipboard, Transferable contents ) {  }
}
